<template>
  <div class="ShopCart">
    <!-- 点击购物车显示，购物车详细 -->
    <transition name="shopDetails">
      <div class="shopDetails" v-if="isShowList">
        <div class="detailsTitle">
          <span>购物车</span>
          <button @click="clearCart">清空</button>
        </div>
        <ul>
          <li v-for="(item,index) in countFoods" :key="index">
            <span>{{item.name}}</span>
            <div>
              <i>￥{{item.specfoods[0].price}}</i>
              <cartControl :food="item"></cartControl>
            </div>
          </li>
        </ul>
      </div>
    </transition>

    <div class="carImg" @click="toggleShow">
      <div class="carWrapper">
        <svg class="icon" aria-hidden="true" :class="{ImgColor:totalCount>0}">
          <use xlink:href="#icon-gouwuche"></use>
        </svg>
        <!-- 总计数 -->
        <span v-if="totalCount">
          <i>{{totalCount}}</i>
        </span>
      </div>
    </div>
    <div class="totalPrice">
      <span>￥{{totalPrice||0}}</span>
      <i>配送费{{shopName.float_delivery_fee}}元</i>
    </div>
    <div class="goPay" :class="{payBg:isMore>=0}">
      <span>{{payText}}</span>
    </div>
  </div>
</template>
<script>
import { mapState, mapGetters } from "vuex";
import { reqshopName } from "../../api/index.js";
import cartControl from "../cartControl/cartControl.vue";
export default {
  data() {
    return {
      shopName: {},
      isShowCart: false
    };
  },
  computed: {
    ...mapState(["countFoods"]),
    ...mapGetters(["totalCount", "totalPrice"]),
    // 判断支付的 背景色
    isMore() {
      const { float_minimum_order_amount } = this.shopName;
      const { totalPrice } = this;
      return totalPrice - float_minimum_order_amount;
    },
    // 判断 支付的text文档
    payText() {
      /* 理解：在this.shopName 中取float_minimum_order_amount */
      const { float_minimum_order_amount } = this.shopName;
      const { totalPrice } = this;
      if (totalPrice === 0) return `${float_minimum_order_amount}元起送`;
      else if (totalPrice < float_minimum_order_amount) {
        return `还差${float_minimum_order_amount - totalPrice}元`;
      } else return "去结算";
    },
    isShowList(){
      if(this.totalCount===0){
        return false
      }
      return this.isShowCart

    }
    ,
    
  },
  async mounted() {
    this.shopName = await reqshopName(this.$route.params.id);
  },
  methods: {
    //清空购物车 操作vuex
    clearCart() {
      this.$store.dispatch("clearCountFoods");
      //隐藏 购物车详细
      this.isShowCart = false;
    },
    toggleShow(){
      if(this.totalCount>0)
      this.isShowCart=!this.isShowCart
    }
  },
  components: {
    cartControl
  }
};
</script>
<style lang="scss" scoped>
/* 动态引用的样式 */
.ImgColor {
  color: rgb(18, 150, 219);
}
.payBg {
  background-color: rgb(18, 150, 219);
}
/* 购物车详细 */
.shopDetails {
  width: 100%;
  height: 40px;
  background-color: #fff;
  position: absolute;
  bottom: 1.2rem;
  display: flex;
  flex-flow: wrap;
  font-size: 0.3rem;
  .detailsTitle {
    width: 100%;
    display: flex;
    justify-content: space-between;
    background-color: #1296d8;
  }
  ul {
    width: 100%;
    display: flex;
    justify-content: space-between;
    background-color: rgb(153, 153, 153);
    flex-flow: wrap;
    li {
      width: 100%;
      display: flex;
      justify-content: space-between;
      div {
        display: flex;
      }
    }
  }
}
/* 购物车样式 */
.ShopCart {
  font-size: 20px;
  width: 100%;
  height: 7vh;
  background-color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  .carImg {
    color: white;
    font-size: 0.55rem;
    .carWrapper {
      position: absolute;
      bottom: 0.1rem;
      left: 0.3rem;
      width: 1rem;
      height: 1rem;
      border: 2px rgba(0, 0, 0, 0.4) solid;
      border-radius: 50%;
      background-color: #555;
      line-height: 1rem;
      text-align: center;
      span {
        width: 0.4rem;
        height: 0.4rem;
        border-radius: 50%;
        background-color: red;
        position: absolute;
        font-size: 0.1rem;
        left: 0.6rem;
        bottom: 0.6rem;
        border: 2px gray solid;
        color: white;
        font-weight: 600;
        line-height: 0.4rem;
      }
    }
  }
  .totalPrice {
    margin-left: 1rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    i {
      font-size: 0.25rem;
    }
  }
  .goPay {
    height: 100%;
    width: 35%;
    display: flex;
    align-items: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* svg引用 */
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
/* 动画 */
.shopDetails-enter-active,
.shopDetails-leave-active {
  transition: all 0.2s ease;
}
.shopDetails-enter,
.shopDetails-leave-to {
  transform: translateY(50px);
  opacity: .3;
}
</style>
